﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;

using Common;
namespace RichECMS.ServerPages.Section
{
    public partial class SectionLeft : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (State.LoginUser.ManagerCompetence > (int)Utility.ManagerCompetence.UserCanIssueAndClients)
            {
                Response.Write("您没有权限查看改页面");
                Response.End();
            }

            if (!IsPostBack)
            {
                if (Request["sid"] != null)
                {
                    if (ValidateCommon.ParamAuth.IsNumber(Request["sid"]))
                    {
                        TreeBing(Convert.ToInt32(Request["sid"]));
                    }
                    else
                    {
                        TreeBing(-1);
                    }
                }
                else
                {
                    TreeBing(-1);
                }
            }
        }


        private void TreeBing(int currentNodeId)
        {
            //加载站点树说明
            TreeNode objNode = new TreeNode();
            objNode.Value = "0";
            objNode.Text = "站点栏目一览";
            objNode.Target = "I3";
            //objNode.NavigateUrl = "SystemManage/SystemInfo.aspx";
            objNode.ImageUrl = "../../sysCommon/sysImages/sectionroot.png";
            SectionTree.Nodes.Add(objNode);

            TreeNode objWebSiteNode = null;
            using (WebSiteFactory objFactory = new WebSiteFactory())
            {
                BusinessLogic.BusinessRules.WebSite objWebSite = objFactory.CreatorBySql(string.Format("intWSState ={0} and intWebSiteID={1} ", (int)Utility.WebSiteState.Use, State.intWSID));
                if (objWebSite != null)
                {
                    foreach (Model.Data.WebSiteData.WebSiteRow objRow in objWebSite.Data.WebSite.Rows)
                    {
                        objWebSiteNode = new TreeNode();
                        objWebSiteNode.Text = objRow.chvName;
                        //objWebSiteNode.NavigateUrl = "WebSite/WebSiteSet.aspx?intWSID=" + objRow.intWebSiteID;
                        objWebSiteNode.Target = "I3";
                        objWebSiteNode.ImageUrl = "../../sysCommon/sysImages/globe.png";
                        objNode.ChildNodes.Add(objWebSiteNode);
                    }
                }
            }

            using (SectionsFactory objFactory = new SectionsFactory())
            {
                BusinessLogic.BusinessRules.Sections objSection = null;
                string strfilter = "";
                if (State.LoginUser.ManagerCompetence > 3)
                {
                    if (State.LoginUser.Permissions != "")
                    {
                        strfilter = " and intSectionID in (" + State.LoginUser.Permissions + ") ";
                    }
                    else
                    {
                        strfilter = " and intSectionID = -100";
                    }
                    objSection = objFactory.CreatorBySql(" intParentID=0 and intWSID=" + State.intWSID + strfilter + "  order by intSortNo desc,  dtmInTime desc ");
                }
                else
                {
                    objSection = objFactory.CreatorBySql("intParentID=0 and intWSID=" + State.intWSID + " order by intSortNo desc,  dtmInTime desc");
                }
                if (objSection != null)
                {
                    TreeNode rootNode = null;
                    foreach (Model.Data.SectionsData.SectionsRow objRow in objSection.Data.Sections.Rows)
                    {
                        rootNode = new TreeNode();
                        rootNode.Text = objRow.chvSTCName + (objRow.IstxtRemarksNull() ? "" : (objRow.txtRemarks.Trim() == "" ? "" : "(" + objRow.txtRemarks + ")"));
                        rootNode.Value = objRow.intSectionID.ToString();
                        rootNode.Target = "I3";
                        if (objRow.intType == (int)Utility.STType.List)
                            rootNode.NavigateUrl = "UpdateSection.aspx?Section=" + objRow.intSectionID;
                        if (objRow.intType == (int)Utility.STType.Content)
                            rootNode.NavigateUrl = "UpdateSection.aspx?Section=" + objRow.intSectionID;

                        if (objRow.intType != (int)Utility.STType.Issue)
                            rootNode.ImageUrl = "../../sysCommon/sysImages/treenode.png";
                        else
                            rootNode.ImageUrl = "../../sysCommon/sysImages/docClosed.png";
                        AddTreeNode(rootNode, objRow.intSectionID);
                        if (objWebSiteNode != null) objWebSiteNode.ChildNodes.Add(rootNode);
                    }
                }
            }

            if (currentNodeId != -1)
            {
                TreeNodeCollection objCollection = SectionTree.Nodes;
                foreach (TreeNode objTreeNode in objCollection)
                {
                    SetNodeExpend(objTreeNode, currentNodeId);
                }
            }
        }
        private void AddTreeNode(TreeNode objNode, int intParentID)
        {
            using (SectionsFactory objFactory = new SectionsFactory())
            {
                BusinessLogic.BusinessRules.Sections objSection = objFactory.CreatorBySql(string.Format("intParentID={0} and intWSID=" + State.intWSID + " order by intSortNo desc, dtmInTime desc", intParentID));
                if (objSection != null)
                {
                    foreach (Model.Data.SectionsData.SectionsRow objRow in objSection.Data.Sections.Rows)
                    {
                        TreeNode rootNode = new TreeNode();
                        rootNode.Text = objRow.chvSTCName + (objRow.IstxtRemarksNull() ? "" : (objRow.txtRemarks.Trim() == "" ? "" : "(" + objRow.txtRemarks + ")"));
                        rootNode.Value = objRow.intSectionID.ToString();
                        rootNode.Target = "I3";
                        if (objRow.intType != (int)Utility.STType.Issue)
                            rootNode.ImageUrl = "../../sysCommon/sysImages/Article.png";
                        else
                            rootNode.ImageUrl = "../../sysCommon/sysImages/docClosed.png";
                        if (objRow.intType == (int)Utility.STType.List)
                            rootNode.NavigateUrl = "UpdateSection.aspx?Section=" + objRow.intSectionID;
                        if (objRow.intType == (int)Utility.STType.Content)
                            rootNode.NavigateUrl = "UpdateSection.aspx?Section=" + objRow.intSectionID;
                        AddTreeNode(rootNode, objRow.intSectionID);
                        objNode.ChildNodes.Add(rootNode);
                    }
                }
            }
        }

        //定位打开节点
        private void SetNodeExpend(TreeNode objNode, int currentID)
        {
            if (objNode.Value == currentID.ToString())
            {
                objNode.Expand();
                FindParentTreeNode(objNode);
            }
            else if (objNode.ChildNodes.Count > 0)
            {
                for (int i = 0; i < objNode.ChildNodes.Count; i++)
                {
                    SetNodeExpend(objNode.ChildNodes[i], currentID);
                }
            }
        }
        private void FindParentTreeNode(TreeNode objNode)
        {
            if (objNode.Parent != null)
            {
                objNode.Parent.Expand();
                FindParentTreeNode(objNode.Parent);
            }
        }
    }
}